
namespace Behemoth.SharePoint.CustomFieldTypes {

    using System;
    using System.Collections.Generic;
    using System.Text;

    using Microsoft.SharePoint;

	public class FlexLookupFieldValueItem { // : SPFieldMultiChoiceValue

		/// <summary>
		/// 
		/// </summary>
		/// <param name="key">The coded primary key value</param>
		/// <param name="text">The readable text that represents the coded value expressed by 'key'</param>
		/// <param name="key2">An optional secondary fieldName. This can be an alternate PK or a second column of multi-column key</param>
		/// <param name="multiColumnFormatString">A standard multi-fieldName value (;# delimited) with three parts</param>
		public FlexLookupFieldValueItem( string key, string text, string key2 ) {
			this.Key = key;
			this.Text = text;
			this.Key2 = key2;
		}
		public FlexLookupFieldValueItem( string key, string text ) : this( key, text, string.Empty ) { }
		public FlexLookupFieldValueItem( string multiColumnFormatString ) {
			multiColumnFormatString = SPFieldMultiColumnValueTools.CanonicalizeMultiColumnFormatString( multiColumnFormatString );
			SPFieldMultiChoiceValue newValue = new SPFieldMultiChoiceValue( multiColumnFormatString );
			this.Key = newValue[0];
			this.Text = newValue[1];
			if (newValue.Count > 2) // *technically* it is optional, so let's treat it that way
				this.Key2 = newValue[2];
		}

		private string _key;
		public string Key {
			get { return _key; }
			set { _key = value; }
		}

		private string _key2;
		public string Key2 {
			get { return _key2; }
			set { _key2 = value; }
		}

		private string _text;
		public string Text {
			get { return _text; }
			set { _text = value; }
		}

		private bool _isMultiFieldKey = false;
		/// <summary>
		/// Default is False. This value determines qhether the secondary key 
		/// will be used in evaulating if two different objects of this type are equal.
		/// If true, Key2 will be compared in addition to Key. 
		/// </summary>'
		/// <remarks>
		/// This value is not retained in ToString, so if you need to change the default
		/// behavior, your logic should set this value after creatong an instance of this class.
		/// </remarks>
		public bool IsMultiFieldKey {
			get { return _isMultiFieldKey; }
			set { _isMultiFieldKey = value; }
		}

		public override bool Equals( object obj ) {
			//return base.Equals( obj );
			if (obj.GetType() == typeof( string )) {
				// For strings, the secondary key is never evaulated.
				return this.Key.Equals( obj.ToString() );
			} else if (obj.GetType() == typeof( FlexLookupFieldValueItem )) {
				FlexLookupFieldValueItem typedObj = (FlexLookupFieldValueItem)obj;
				return (this.Key.Equals( typedObj.Key ) && (!IsMultiFieldKey || this.Key2.Equals( typedObj.Key2 )));
			}
			throw new NotSupportedException( "Types other than string and FlexLookupFieldValueItem are not supported for comparison." );
		}
		public override int GetHashCode() {
			return base.GetHashCode();
			//return this.Key.GetHashCode();
		}
		public override string ToString() {
			SPFieldMultiChoiceValue newValue = new SPFieldMultiChoiceValue();
			newValue.Add( this.Key );
			newValue.Add( this.Text );
			newValue.Add( this.Key2 );
			return newValue.ToString();
		}

	} // class

} // namespace
